package com.pureut.equipment.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pureut.equipment.domain.dto.InspectionItemPlanDto;
import com.pureut.equipment.domain.vo.InspectionItemPlanVo;
import com.pureut.equipment.service.InspectionItemPlanService;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.List;

/***
 *
 * Author:点检计划控制层
 * Date:2023/04/25 10:12
 * @DESC
 */
@RestController
@RequestMapping("/inspection-item-plan")
public class InspectionItemPlanController extends BaseController {

    /**
     * 点检计划service
     */
    @Resource
    InspectionItemPlanService inspectionItemPlanService;

    /**
     * 点检计划列表
     *
     * @param inspectionItemPlanVo
     * @return
     */
    @SaCheckPermission("device:spotCheck:plan:list")
    @GetMapping("/list")
    public TableDataInfo<InspectionItemPlanDto> list(InspectionItemPlanVo inspectionItemPlanVo, PageQuery pageQuery) {
        return inspectionItemPlanService.getList(inspectionItemPlanVo,pageQuery);
    }

    /**
     * 新增
     */
    @SaCheckPermission("device:spotCheck:plan:add")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> add(@Validated @RequestBody InspectionItemPlanVo inspectionItemPlanVo) throws Exception {
        return toAjax(inspectionItemPlanService.add(inspectionItemPlanVo));
    }

    /**
     * 删除
     */
    @SaCheckPermission("device:spotCheck:plan:remove")
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{id}")
    public R<Void> add(@PathVariable("id") Long id) {
        return toAjax(inspectionItemPlanService.deleteInspectionItemPlan(id));
    }

    /**
     * 修改
     */
    @SaCheckPermission("device:spotCheck:plan:edit")
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping("/updateItemPlan")
    public R<Void> updateItemPlan(@RequestBody InspectionItemPlanVo inspectionItemPlanVo) {
        return toAjax(inspectionItemPlanService.updateItemPlan(inspectionItemPlanVo));
    }

    /**
     * 查看
     */
    @SaCheckPermission("device:spotCheck:plan:query")
    @GetMapping("/getView/{id}")
    public R<InspectionItemPlanDto> getView(@PathVariable("id") Long id) {
        return R.ok(inspectionItemPlanService.getItemPlanView(id));
    }

    /**
     * 启用
     */
    @SaCheckPermission("device:spotCheck:plan:enable")
    @GetMapping("/enable/{id}")
    public R<Void> enable(@PathVariable("id") Long id) throws Exception {
        return toAjax(inspectionItemPlanService.enable(id));
    }

    /**
     * 停用
     */
    @SaCheckPermission("device:spotCheck:plan:stop")
    @GetMapping("/stop/{id}")
    public R<Void> stop(@PathVariable("id") Long id) {
        return toAjax(inspectionItemPlanService.stop(id));
    }


    /**
     * 根据计划开始时间和结束时间定时修改计划状态(定时任务调用)
     */
    @PostMapping("/updateInspectionStatusByTime")
    public R<Void> updateInspectionStatusByTime() throws ParseException {
        return toAjax(inspectionItemPlanService.updateInspectionStatusByTime());
    }
}
